// pages/addressManage/addressManage.js
var call = require("../../../utils/util");
var tcity = require("../../../utils/citys");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: []
  },
  returnMenu() {
    wx.navigateBack({
      delta: 1,
    })
  },
  iconClick(e) {
    let id = e.currentTarget.dataset.id;
    let tempobj = this.data.addressList[e.currentTarget.dataset.index];
    // console.log(tempobj);
    wx.navigateTo({
      url: '../addAddress/addAddress?id=' + id + '&name=' + tempobj.name + '&phone=' + tempobj.phone + '&address=' + tempobj.address + '&status=' + tempobj.status + '&pNumber=' + tempobj.pNumber + '&cNumber=' + tempobj.cNumber + '&aNumber=' + tempobj.aNumber
    })
  },
  // 长按执行删除操作
  goSetLongClick(e) {
    // let that = this;
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.showModal({
      title: '删除确认',
      content: '您确定要删除该地址吗',
      confirmText: '删除',
      success:(res) => {
        if (res.cancel) {
          // 用户点击了取消属性的按钮

        } else if (res.confirm) {
          // 用户点击了确定属性的按钮
          let token = wx.getStorageSync('token');
          wx.request({
            url: 'http://api_devs.wanxikeji.cn/api/userAddressDelete',
            method: 'POST',
            data: {
              'token': token,
              'id': id
            },
            success: (res) =>{
              if (res) {
                wx.request({
                  url: 'http://api_devs.wanxikeji.cn/api/userAddressList',
                  method: 'POST',
                  data: {
                    'token': token,
                  },
                  success: (res) => {
                    // console.log(res.data.data);
                    // console.log(this.data.addressList);
                    tcity.init(this);
                    var cityData = this.data.cityData;
                    var tempList = [];
                    // console.log(data.data);
                    for (let i = 0; i < res.data.data.length; i++) {
                      let obj = {};
                      let temp1 = "";
                      let temp2 = "";
                      let temp3 = "";
                      let temp4 = "";
                      obj["name"] = res.data.data[i].name;
                      obj["phone"] = res.data.data[i].phone;
                    
                      //筛选省
                      temp1 = cityData.filter((value) => {
                        if (value.code == res.data.data[i].procince)
                          return value;
                      })[0];
                      //筛选市
                      temp2 = (temp1.sub).filter((value) => {
                        if (value.code == res.data.data[i].city)
                          return value;
                      })[0];
                      //筛选区
                      temp3 = (temp2.sub).filter((value) => {
                        if (value.code == res.data.data[i].area)
                          return value;
                      })[0];
                    
                      temp4 = res.data.data[i].detailed;
                    
                      obj["status"] = res.data.data[i].default;
                      obj["address"] = temp1.name + '-' + temp2.name + '-' + temp3.name + ' ' + temp4;
                      obj["address_id"] = res.data.data[i].address_id;
                      obj["pNumber"] = res.data.data[i].procince;
                      obj["cNumber"] = res.data.data[i].city;
                      obj["aNumber"] = res.data.data[i].area;
                      tempList.push(obj);
                    }
                    this.setData({
                      addressList: tempList
                    });
                  }
                });
              }
            }
          });
        }
      }
    });


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow: function () {
    // 请求接口
    let token = wx.getStorageSync('token');
    call.request(
      'api/userAddressList',
      {
        'token': token,
      },
      this.getsuccess,
      this.error);
  },
  getsuccess(data) {
    var that = this;
    tcity.init(that);
    var cityData = that.data.cityData;
    var tempList = [];
    // console.log(data.data);
    for (let i = 0; i < data.data.length; i++) {
      let obj = {};
      let temp1 = "";
      let temp2 = "";
      let temp3 = "";
      let temp4 = "";
      obj["name"] = data.data[i].name;
      obj["phone"] = data.data[i].phone;

      //筛选省
      temp1 = cityData.filter((value) => {
        if (value.code == data.data[i].procince)
          return value;
      })[0];
      //筛选市
      temp2 = (temp1.sub).filter((value) => {
        if (value.code == data.data[i].city)
          return value;
      })[0];
      //筛选区
      temp3 = (temp2.sub).filter((value) => {
        if (value.code == data.data[i].area)
          return value;
      })[0];

      temp4 = data.data[i].detailed;

      obj["status"] = data.data[i].default;
      obj["address"] = temp1.name + '-' + temp2.name + '-' + temp3.name + ' ' + temp4;
      obj["address_id"] = data.data[i].address_id;
      obj["pNumber"] = data.data[i].procince;
      obj["cNumber"] = data.data[i].city;
      obj["aNumber"] = data.data[i].area;
      tempList.push(obj);
    }
    this.setData({
      addressList: tempList
    });
    // console.log(this.data.addressList);
  },
})